package ensib.sti.ssu.activities;

 
import ensib.sti.ssu.R;
import ensib.sti.ssu.DAO.BlocNoteDAO;
import ensib.sti.ssu.beans.BlocNote;
import android.annotation.SuppressLint;
import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.util.Log;
import android.view.KeyEvent;
import android.view.View;
import android.view.View.OnClickListener;
 
import android.widget.ArrayAdapter;
import android.widget.Button;
import android.widget.ImageButton;
import android.widget.Spinner;
import android.widget.TextView;
import android.widget.Toast;

public class AjoutNoteActivity extends Activity {
	private static int TAKE_PICTURE_CODE=0x900; 
	private String typetache="PERSO",priorite="BASSE";
	private String photourl=null;
 private boolean date_pick=false;
	protected int PICK_DATE_CODE=0x901;
	private TextView dateview;
	@Override
	protected void onCreate(Bundle savedInstanceState) {
		// TODO Auto-generated method stub
		super.onCreate(savedInstanceState);
		setContentView(R.layout.ajoutnote);
	    
	     final TextView   description=(TextView)  findViewById(R.id.ajout_description),
	    		     nomtacheview=(TextView)  findViewById(R.id.ajout_nomtache);
	    		     dateview=(TextView)  findViewById(R.id.ajout_date);
	     Button valider=(Button) findViewById(R.id.ajout_valider_bouton),
	    		 annuler=(Button) findViewById(R.id.ajout_annuler_bouton);
	           ImageButton  takephoto=(ImageButton) findViewById(R.id.ajouter_blocnote_take_photo);
	      final Spinner  priorite= (Spinner)  findViewById(R.id.ajout_priorite);
	  	final Spinner typetache=(Spinner)  findViewById(R.id.ajout_typetache);
	  	dateview.setOnKeyListener(new View.OnKeyListener() {
			
			@Override
			public boolean onKey(View v, int keyCode, KeyEvent event) {
				// TODO Auto-generated method stub
				 if(!date_pick)
				startActivityForResult(new Intent(AjoutNoteActivity.this, PickDateActivity.class), PICK_DATE_CODE);
                  date_pick=true;
				return true;
			}
		});
		ArrayAdapter adapter = ArrayAdapter.createFromResource(this, R.array.priorite_arrays, R.layout.prioritespinner),
		
		adapter2 = ArrayAdapter.createFromResource(this, R.array.type_tache_arrays, R.layout.prioritespinner);
		
		adapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
		adapter2.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
		typetache.setAdapter(adapter2);
		priorite.setAdapter(adapter);
annuler.setOnClickListener(new View.OnClickListener() {
			
			@Override
			public void onClick(View v) {

				finish(); 
			}
		});
valider.setOnClickListener(new View.OnClickListener() {
			
			@SuppressLint("ShowToast")
			@Override
			public void onClick(View v) {
BlocNoteDAO dao = new  BlocNoteDAO(AjoutNoteActivity.this.getApplicationContext());
BlocNote note=new BlocNote();
String date=dateview.getText().toString().trim();
Log.i("date_picker", date);

note.setDeadLineDate(date);
Log.i("date_picker_deadlineset", note.getDeadLineDate()+"");
Log.i("date_picker_deadlineset_to_string", note.getDeadLineDateToString());

note.setDescription(description.getText().toString());
note.setNomTache(nomtacheview.getText().toString());
note.setPhotoUrl(AjoutNoteActivity.this.photourl);
AjoutNoteActivity.this.priorite=(String) priorite.getSelectedItem();
AjoutNoteActivity.this.typetache=(String) typetache.getSelectedItem();
note.setPriorite(AjoutNoteActivity.this.priorite);
note.setTypeTache(AjoutNoteActivity.this.typetache);
		dao.open();
		dao.insertBlocNote(note);
		dao.close();
		Toast.makeText(getApplicationContext(), AjoutNoteActivity.this.getResources().getString(R.string.message_succes), Toast.LENGTH_LONG);
		finish();
			}
		});
		takephoto.setOnClickListener(new OnClickListener() {
			
			@Override
			public void onClick(View v) {

				// TODO Auto-generated method stub
				startActivityForResult(new Intent(AjoutNoteActivity.this, PrendrePhotoActivity.class), TAKE_PICTURE_CODE);

			}
		});
		  
		 
	}
	@Override
	protected void onActivityResult(int requestCode, int resultCode, Intent data) {
	    if (resultCode == RESULT_OK ){
	    	if(requestCode==TAKE_PICTURE_CODE){
	        if (data.hasExtra("imageurl")) {
	             photourl=data.getExtras().getString("imageurl");
	             
	             TextView v=(TextView) AjoutNoteActivity.this.findViewById(R.id.phototaken_url_show);
	             v.setVisibility(TextView.VISIBLE);
	             
	             Log.i("take_photo", photourl);
	        }
	    }}
	    
	    if(requestCode==PICK_DATE_CODE){
	    	if (data.hasExtra("date")) {
	    	  dateview.setText(data.getExtras().getString("date"));
	    	  date_pick=false;
	    	}
	    }
	    
	}
	@Override
	public void finish() {
		// TODO Auto-generated method stub
		setResult(RESULT_OK);
		super.finish();
	}
}
